package com.lzy.chat.d_controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzy.chat.a_entity.User;
import com.lzy.chat.c_service.Exception.Exceptions;
import com.lzy.chat.c_service.Exception.ServiceException;
import com.lzy.chat.c_service.LoginContext;
import com.lzy.chat.c_service.LoginService;
import com.lzy.chat.c_service.TokenService;
import com.lzy.chat.c_service.UserService;
import com.lzy.chat.e_util.JsonResult;
import com.lzy.chat.g_annotation.CurrentUser;
import com.lzy.chat.g_annotation.NeedToken;
import com.lzy.chat.g_annotation.PassToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@RestController
@RequestMapping("user")
public class UserController extends BaseController {

    private final UserService userService;
    private final TokenService tokenService;
    @Qualifier("PasswordLogin")
    private final LoginService loginService1;
    @Qualifier("VerifyLogin")
    private final LoginService loginService2;


    @Autowired
    public UserController(UserService user, TokenService token, @Qualifier("PasswordLogin") LoginService loginService1, @Qualifier("VerifyLogin") LoginService loginService2) {
        this.userService = user;
        this.tokenService = token;
        this.loginService1 = loginService1;
        this.loginService2 = loginService2;
    }

    @PassToken //不需要Token直接放行
    @RequestMapping("login")
    public JsonResult<Map<String, Object>> login(Integer id, String password, String type) {
        User user;
        LoginContext loginContext;
        if (type.equals("0")) {
            loginContext = new LoginContext(loginService1);
        } else {
            loginContext = new LoginContext(loginService2);
        }
        user = loginContext.execute(id, password);
        String token = tokenService.getToken(user);
        user.setPassword(null);
        user.setSalt(null);//去除敏感信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("token", token);
        return new JsonResult<>(ok, map);
    }

    @PassToken
    @RequestMapping("updateToken")
    public JsonResult<Map<String, String>> updateToken(String token) {
        int uid = tokenService.getUidFromToken(token);
        User user = userService.getInfoById(uid);
        if (user == null) throw new ServiceException(Exceptions.UserNotFoundException);
        if (!tokenService.verifyToken(user, token))
            throw new ServiceException(Exceptions.TokenVerifyException);
        String token1 = tokenService.getToken(user);
        HashMap<String, String> map = new HashMap<>();
        map.put("token", token1);
        return new JsonResult<>(ok, map);
    }

    @PassToken
    @RequestMapping("verify")
    public JsonResult<Map<String, Object>> verify(String email, String phone, Integer type) {
        return new JsonResult<>(ok, userService.verify(email, phone, type));
    }

    @PassToken
    @RequestMapping("register")
    public JsonResult<Map<String, Object>> register(User user) {
        Integer id = userService.register(user);
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        return new JsonResult<>(ok, map);
    }

    @NeedToken
    @RequestMapping("updateUserInfo")
    public JsonResult<Void> updateUserInfo(User user, @CurrentUser User u) {
//        System.out.println(user);
        Integer id = u.getId();
        if (!Objects.equals(id, user.getId())) return new JsonResult<>(5006, "用户ID与信息不匹配");
        userService.updateUserInfo(user);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("updateUserPassword")
    public JsonResult<Void> updateUserPassword(String oldPassword, String newPassword, @CurrentUser User user) {
        userService.updateUserPassword(newPassword, oldPassword, user);
        return new JsonResult<>(ok);
    }

    @PassToken
    @RequestMapping("findUserPassword")
    public JsonResult<Void> findUserPassword(String newPassword, Integer id) {
        userService.findUserPassword(newPassword, id);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("search")
    public JsonResult<List<User>> searchUsers(String keyword, Integer index) {
        PageHelper.startPage(index, 10);
        List<User> list = userService.getUsersByIdOrName(keyword);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        JsonResult<List<User>> jsonResult = new JsonResult<>();
        jsonResult.setState(ok);
        jsonResult.setMessage(pageInfo.isHasNextPage() + "");
        jsonResult.setData(list);
        return jsonResult;
    }

    @NeedToken
    @RequestMapping("getUserInfoById")
    public JsonResult<User> getUserInfoById(Integer id) {
        User user = userService.getInfoById(id);
        if (user == null) throw new ServiceException(Exceptions.UserNotFoundException);
        user.setPassword(null);
        user.setSalt(null);
        return new JsonResult<>(ok, user);
    }


    @NeedToken //需要Token的api
    @RequestMapping("test")
    public JsonResult<User> test(@CurrentUser User user) {
        return new JsonResult<>(ok, user);
    }
}
